package org.itheima.service.impl;

import org.itheima.mapper.UserMapper;
import org.itheima.pojo.User;
import org.itheima.service.UserService;
import org.itheima.utils.Md5Util;
import org.itheima.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User findByUserName(String username) {
        User u =userMapper.findByUserName(username);
        System.out.println(u);
        return u;
    }

    @Override
    public void register(String username,String password) {
            String md5String = Md5Util.getMD5String(password);
            userMapper.add(username, md5String);
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateAvatar(avatarUrl,id);
    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updatePwd(Md5Util.getMD5String(newPwd),id);
    }

    @Override
    public void updatePersonalIntroductionAndEmail(String newBio,String newEmail) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updatePersonalIntroductionAndEmail(newBio,newEmail,id);
    }

    @Override
    public void updateNickname(String nickname) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateNickname(nickname, id);
    }

    @Override
    public List<Map<String, Object>> findAll() {
        return userMapper.findAll();
    }

    @Override
    public void updateStatus(int id) {
        userMapper.updateStatus(id);
    }

    @Override
    public void updateStatus2(int id) {
        userMapper.updateStatus2(id);
    }

    @Override
    public List<Map<String, Object>> findAllOrdinary() {
        return userMapper.findAllOrdinary();
    }

}
